package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.RelationsDAO;
import org.gap.dao.RelationsTiersDAO;

import org.gap.domain.Relations;
import org.gap.domain.RelationsTiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Relations entities
 * 
 */

@Service("RelationsService")
@Transactional
public class RelationsServiceImpl implements RelationsService {

	/**
	 * DAO injected by Spring that manages Relations entities
	 * 
	 */
	@Autowired
	private RelationsDAO relationsDAO;

	/**
	 * DAO injected by Spring that manages RelationsTiers entities
	 * 
	 */
	@Autowired
	private RelationsTiersDAO relationsTiersDAO;

	/**
	 * Instantiates a new RelationsServiceImpl.
	 *
	 */
	public RelationsServiceImpl() {
	}

	/**
	 * Return all Relations entity
	 * 
	 */
	@Transactional
	public List<Relations> findAllRelationss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Relations>(relationsDAO.findAllRelationss(startResult, maxRows));
	}

	/**
	 * Save an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Relations saveRelationsRelationsTierses(String relRelation, RelationsTiers related_relationstierses) {
		Relations relations = relationsDAO.findRelationsByPrimaryKey(relRelation, -1, -1);
		RelationsTiers existingrelationsTierses = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstierses.getRltTiersParent(), related_relationstierses.getRltTiersFils(), related_relationstierses.getRltRelation());

		// copy into the existing record to preserve existing relationships
		if (existingrelationsTierses != null) {
			existingrelationsTierses.setRltTiersParent(related_relationstierses.getRltTiersParent());
			existingrelationsTierses.setRltTiersFils(related_relationstierses.getRltTiersFils());
			existingrelationsTierses.setRltRelation(related_relationstierses.getRltRelation());
			existingrelationsTierses.setRltSens(related_relationstierses.getRltSens());
			existingrelationsTierses.setRltDateDebut(related_relationstierses.getRltDateDebut());
			existingrelationsTierses.setRltDateFin(related_relationstierses.getRltDateFin());
			related_relationstierses = existingrelationsTierses;
		}

		related_relationstierses.setRelations(relations);
		relations.getRelationsTierses().add(related_relationstierses);
		related_relationstierses = relationsTiersDAO.store(related_relationstierses);
		relationsTiersDAO.flush();

		relations = relationsDAO.store(relations);
		relationsDAO.flush();

		return relations;
	}

	/**
	 */
	@Transactional
	public Relations findRelationsByPrimaryKey(String relRelation) {
		return relationsDAO.findRelationsByPrimaryKey(relRelation);
	}

	/**
	 * Load an existing Relations entity
	 * 
	 */
	@Transactional
	public Set<Relations> loadRelationss() {
		return relationsDAO.findAllRelationss();
	}

	/**
	 * Return a count of all Relations entity
	 * 
	 */
	@Transactional
	public Integer countRelationss() {
		return ((Long) relationsDAO.createQuerySingleResult("select count(o) from Relations o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Relations entity
	 * 
	 */
	@Transactional
	public void deleteRelations(Relations relations) {
		relationsDAO.remove(relations);
		relationsDAO.flush();
	}

	/**
	 * Save an existing Relations entity
	 * 
	 */
	@Transactional
	public void saveRelations(Relations relations) {
		Relations existingRelations = relationsDAO.findRelationsByPrimaryKey(relations.getRelRelation());

		if (existingRelations != null) {
			if (existingRelations != relations) {
				existingRelations.setRelRelation(relations.getRelRelation());
				existingRelations.setRelLibelleDesc(relations.getRelLibelleDesc());
				existingRelations.setRelLibelleAsc(relations.getRelLibelleAsc());
				existingRelations.setRelType(relations.getRelType());
			}
			relations = relationsDAO.store(existingRelations);
		} else {
			relations = relationsDAO.store(relations);
		}
		relationsDAO.flush();
	}

	/**
	 * Delete an existing RelationsTiers entity
	 * 
	 */
	@Transactional
	public Relations deleteRelationsRelationsTierses(String relations_relRelation, Integer related_relationstierses_rltTiersParent, Integer related_relationstierses_rltTiersFils, String related_relationstierses_rltRelation) {
		RelationsTiers related_relationstierses = relationsTiersDAO.findRelationsTiersByPrimaryKey(related_relationstierses_rltTiersParent, related_relationstierses_rltTiersFils, related_relationstierses_rltRelation, -1, -1);

		Relations relations = relationsDAO.findRelationsByPrimaryKey(relations_relRelation, -1, -1);

		related_relationstierses.setRelations(null);
		relations.getRelationsTierses().remove(related_relationstierses);

		relationsTiersDAO.remove(related_relationstierses);
		relationsTiersDAO.flush();

		return relations;
	}
}
